package www.openteam.com.model.policy;

import android.support.annotation.NonNull;

import java.util.List;

import www.openteam.com.OTApplication;
import www.openteam.com.base.BaseModel;
import www.openteam.com.bean.ApprovalStatu;
import www.openteam.com.bean.ProOrder;
import www.openteam.com.exception.ApiException;
import www.openteam.com.subscribe.CommonSubscriber;
import www.openteam.com.transformer.CommonTransformer;

/**
 * Created by kylin on 2017/1/16.
 */

public class PurchaseOrderApprovalModel extends BaseModel {

    private boolean isQuery = false;

    private boolean isApproval = false;

    private boolean isUnApproval =true;
//    queryPrOrder(@Query("rdsno") String rdsno, @Query("") String code,
// @Query("orderStatus") String orderStatus, @Query("startDate") long startDate,
// @Query("endDate") long endDate, @Query("pageSize") int PageSize);


    public boolean queryPrOrder(@NonNull String rdsno,
                              @NonNull  String code,
                              @NonNull String orderStatus,
                              long startDate,
                              long endDate,
                                String stype, String moreCom,
                              @NonNull int PageSize, @NonNull final
                              PurchaseOrderApprovalOnListener purchaseOrderApprovalOnListener) {
        httpApiService.queryPrOrder(rdsno, code, orderStatus, startDate, endDate,stype,moreCom,PageSize)
                .compose(new CommonTransformer<ProOrder>())
                .subscribe(new CommonSubscriber<ProOrder>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(ProOrder proOrder) {
                        isQuery = true;
                        purchaseOrderApprovalOnListener.queryPOASuccess(proOrder.getData());
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isQuery = false;
                        purchaseOrderApprovalOnListener.queryPOAFail(e.message);
                    }
                });
        return isQuery;
    }


    public boolean prApprovalUpdate(@NonNull int id, @NonNull final PurchaseOrderAOnListener purchaseOrderAOnListener) {
        httpApiService.prApprovalUpdate(id)
                .compose(new CommonTransformer<ApprovalStatu>())
                .subscribe(new CommonSubscriber<ApprovalStatu>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(ApprovalStatu approvalStatu) {
                        isApproval =true;
                        purchaseOrderAOnListener.approvalPOASuccess(approvalStatu);
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isApproval= false;
                        purchaseOrderAOnListener.approvalPOAFail(e.message);
                    }
                });
        return  isApproval;
    }


    public boolean unprApprovalUpdate(@NonNull int id, @NonNull final PurchaseOrderUOnListener customOrderUOnListener) {
        httpApiService.unprApprovalUpdate(id)
                .compose(new CommonTransformer<ApprovalStatu>())
                .subscribe(new CommonSubscriber<ApprovalStatu>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(ApprovalStatu approvalStatu) {
                        isApproval =true;
                        customOrderUOnListener.unApprovalPOASuccess(approvalStatu);
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isApproval= false;
                        customOrderUOnListener.unApprovalPOAFail(e.message);
                    }
                });
        return  isApproval;
    }

    public interface PurchaseOrderApprovalOnListener {
        void queryPOASuccess(List<ProOrder.DataEntity> orderApprovals);

        void queryPOAFail(String failMsg);
    }

    public interface  PurchaseOrderAOnListener{
        void approvalPOASuccess(ApprovalStatu aprovalStatu);
        void approvalPOAFail(String failMsg);

    }

    public interface  PurchaseOrderUOnListener{
        void unApprovalPOASuccess(ApprovalStatu aprovalStatu);
        void unApprovalPOAFail(String failMsg);
    }
}
